package com.kbt.adapter;

import android.app.Activity;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import android.widget.RadioGroup;
import android.widget.Toast;
import com.kbt.activity.R;
import com.kbt.util.utils.ImageManager;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


public class GoodInfoAdapter extends BaseAdapter {

    private List<String> goodInfoList;
    private Activity context;
    private ImageLoader imageLoader;

    public GoodInfoAdapter(Activity context, List<String> goodInfoList) {
        this.goodInfoList = goodInfoList;
        this.context = context;
        this.imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return goodInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * �ٴθ�ֵ
     *
     * @param mlist
     */
    public void setList(List<String> mlist) {
        goodInfoList = mlist;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        H itemHolder=null;
        if (convertView == null) {
            itemHolder = new H();
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.good_info_image_item, null);
            itemHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (H) convertView.getTag();
        }
        String url = goodInfoList.get(position).replaceAll("\"","");

        int screenWidth = context.getWindowManager().getDefaultDisplay().getWidth();
        ViewGroup.LayoutParams lp = itemHolder.imageView.getLayoutParams();
        lp.width = screenWidth;

        String[] url_params = url.split("&");

        int imageWidth = 0;
        int imageHeight = 0;
        if(url_params.length == 3){
            imageWidth = Integer.parseInt(url_params[1].split("=")[1]);
            imageHeight = Integer.parseInt(url_params[2].split("=")[1]);
        }

        if(imageWidth>0 && imageHeight>0){
            lp.height = new Float(imageHeight*screenWidth/imageWidth).intValue();
        }else{
            lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        itemHolder.imageView.setLayoutParams(lp);
        itemHolder.imageView.setMaxWidth(screenWidth);
        itemHolder.imageView.setMaxHeight(screenWidth *10);
                imageLoader.displayImage(url, itemHolder.imageView, ImageManager.getOptions(
                R.drawable.loadings, R.drawable.loadings));




        return convertView;
    }
    public static class H {
        public ImageView imageView;
    }
}
